package com.hspedu.poly.polyarr;

public class PolyArray {
    public static void main(String[] args) {
        Person [] persons = new Person[5];
        persons[0] = new Person("jack",20);
        persons[1] = new Student("jack",18,100);
        persons[2] = new Student("smith",20,99.5);
        persons[3] = new Teacher("Miss Li",30,20000);
        persons[4] = new Teacher("Mr wang",40,50000);

        for (int i = 0; i < persons.length ; i++) {
            System.out.println(persons[i].say());
            //这里有对persons[i]的运行类型进行判断(instanceof)，所以可以转，不会抛出异常
            if(persons[i] instanceof Student){
                ((Student) persons[i]).study();
            }else if(persons[i] instanceof Teacher){
                ((Teacher) persons[i]).teach();
            }else if(persons[i] instanceof Person){

            }else{
                System.out.println("您的类型有误，请检查。");
            }
            //下面两行代码会报错，因为多态数组的编译类型是父类，所以没办法使用子类的特有方法
            //persons[i].teach();
            //persons[i].study();
        }


    }
}
